package com.ncp.servlet.registerlogin;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ncp.model.biz.registerlogin.UserManager;
import com.ncp.model.entity.registerlogin.UserBasicBean;

public class UserClServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");
		String type=request.getParameter("type");
		if(type.equals("del")){
			String id=request.getParameter("id");
			boolean b=new UserManager().delUser(id);
			if(b){
				response.sendRedirect("pages/admin/userlist.jsp");
			}
		}else if(type.equals("update")){
			int id=Integer.parseInt(request.getParameter("id"));
			String loginname=request.getParameter("loginname");
			String password=request.getParameter("password");
			int level=Integer.parseInt(request.getParameter("level"));
			String username=request.getParameter("username");
			String email=request.getParameter("email");
			String tel=request.getParameter("tel");
			String address=request.getParameter("address");
			String date=request.getParameter("date");
			String status=request.getParameter("status");
			UserBasicBean ubb=new UserBasicBean();
			ubb.setUserID(id);
			ubb.setUserLoginName(loginname);
			ubb.setPassWord(password);
			ubb.setLevelID(level);
			ubb.setUserName(username);
			ubb.setEmail(email);
			ubb.setTel(tel);
			ubb.setAddress(address);
			ubb.setRegisterDate(date);
			ubb.setStatus(status);
			request.setAttribute("ubb", ubb);
			request.getRequestDispatcher("pages/admin/userupdate.jsp").forward(request, response);
		}else if(type.equals("update1")){
			String id=request.getParameter("id");
			String status=request.getParameter("status");
			String level=request.getParameter("id");
			new UserManager().updateUser(id, level, status);
			request.getRequestDispatcher("pages/admin/userlist.jsp").forward(request, response);
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html");
		this.doGet(request, response);
	}

}
